package com.achobeta.recruitment.controller.interact;

import com.achobeta.recruitment.common.AppResult;
import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.common.Rule;
import com.achobeta.recruitment.model.Manager;
import com.achobeta.recruitment.model.PageConfig;
import com.achobeta.recruitment.service.interact.PageConfigService;
import com.achobeta.recruitment.utils.CheckUtils;
import com.achobeta.recruitment.utils.LimitUtils;
import com.achobeta.recruitment.utils.MediaUtils;
import com.achobeta.recruitment.utils.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-31
 * Time: 12:59
 */
@Api(tags = "页面配置信息相关")
@Slf4j
@RestController
@RequestMapping("/config")
public class PageConfigController {

    @Autowired
    private PageConfigService pageConfigService;

    // 任何人都可以获取，公共的
    @ApiOperation(value = "获取保存了的配置信息", notes = "data为当前保存了的配置信息，PageConfig")
    @GetMapping("/real")
    public AppResult<PageConfig> selectReal() {
        // 调用dao层
        PageConfig pageConfig = pageConfigService.selectReal();
        if(pageConfig == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        // 成功则返回
        return AppResult.success(pageConfig);
    }

    // 任何人都可以获取，公共的
    @ApiOperation(value = "获取未保存的配置信息", notes = "data为预览下的配置信息，PageConfig")
    @GetMapping("/virtual")
    public AppResult<PageConfig> selectVirtual(HttpServletRequest request) {
        // 获取当前登录用户
        Manager manager = SessionUtils.getManager(request);
        // 判断是否有权限
        if(!LimitUtils.hasPower(manager, Rule.GET_VIRTUAL_CONFIG_RULE)) {
            log.warn(ResultCode.FAILED_MANAGER_NOT_POWERS.toString());
            return AppResult.failed(ResultCode.FAILED_MANAGER_NOT_POWERS);
        }
        // 调用dao层
        PageConfig pageConfig = pageConfigService.selectVirtual();
        if(pageConfig == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        // 成功则返回
        return AppResult.success(pageConfig);
    }

    @ApiOperation(value = "上传首页配置", notes = "返回的data为配置保存对象，包含文件名和大小")
    @PostMapping("/home")
    public AppResult<String> uploadHomePhoto(HttpServletRequest request,
                                     @ApiParam("首页配置") @NonNull @RequestPart("homePhoto") MultipartFile file) {
        CheckUtils.checkNULL(file);
        // 获取当前登录用户
        Manager manager = SessionUtils.getManager(request);
        // 判断是否有权限
        if(!LimitUtils.hasPower(manager, Rule.UPLOAD_CONFIG_RULE)) {
            log.warn(ResultCode.FAILED_MANAGER_NOT_POWERS.toString());
            return AppResult.failed(ResultCode.FAILED_MANAGER_NOT_POWERS);
        }
        // 获取一个路径
        String originName = file.getOriginalFilename();
        // 校验类型
        CheckUtils.checkImage(originName.substring(originName.lastIndexOf(".")));
        String path = MediaUtils.getImageUniquePath(originName);
        CheckUtils.checkNULL(path);
        // 调用service层
        PageConfig pageConfig = pageConfigService.uploadHomePhoto(file, path);
        // 返回成功（路径）
        return AppResult.success("上传成功", path);
//        return AppResult.success("上传成功", pageConfig);
    }

    @ApiOperation(value = "上传投递页配置", notes = "返回的data为配置对象，包含文件名和大小")
    @PostMapping("/deliver")
    public AppResult<String> uploadDeliverPhoto(HttpServletRequest request,
                                     @ApiParam("投递页配置") @NonNull @RequestPart("deliverPhoto") MultipartFile file) {
        CheckUtils.checkNULL(file);
        // 获取当前登录用户
        Manager manager = SessionUtils.getManager(request);
        // 判断是否有权限
        if(!LimitUtils.hasPower(manager, Rule.UPLOAD_CONFIG_RULE)) {
            log.warn(ResultCode.FAILED_MANAGER_NOT_POWERS.toString());
            return AppResult.failed(ResultCode.FAILED_MANAGER_NOT_POWERS);
        }
        // 获取一个路径
        String originName = file.getOriginalFilename();
        // 校验类型
        CheckUtils.checkImage(originName.substring(originName.lastIndexOf(".")));
        String path = MediaUtils.getImageUniquePath(originName);
        CheckUtils.checkNULL(path);
        // 调用service层
        PageConfig pageConfig = pageConfigService.uploadDeliverPhoto(file, path);
        // 返回成功（路径）
        return AppResult.success("上传成功", path);
//        return AppResult.success("上传成功", pageConfig);
    }

    @ApiOperation(value = "上传简历模板", notes = "返回的data为配置对象，包含文件名")
    @PostMapping("/model")
    public AppResult<String> uploadModel(HttpServletRequest request,
                                                @ApiParam("简历模板") @NonNull @RequestPart("model") MultipartFile file) {
        CheckUtils.checkNULL(file);
        // 获取当前登录用户
        Manager manager = SessionUtils.getManager(request);
        // 判断是否有权限
        if(!LimitUtils.hasPower(manager, Rule.UPLOAD_CONFIG_RULE)) {
            log.warn(ResultCode.FAILED_MANAGER_NOT_POWERS.toString());
            return AppResult.failed(ResultCode.FAILED_MANAGER_NOT_POWERS);
        }
        // 获取一个路径
        String path = MediaUtils.getImageUniquePath(file.getOriginalFilename());
        CheckUtils.checkNULL(path);
        // 调用service层
        PageConfig pageConfig = pageConfigService.uploadModel(file, path);
        // 返回成功（路径）
        return AppResult.success("上传成功", path);
//        return AppResult.success("上传成功", pageConfig);
    }

    @ApiOperation(value = "保存配置", notes = "无data")
    @GetMapping("/save")
    public AppResult saveConfig(HttpServletRequest request) {
        // 获取当前登录用户
        Manager manager = SessionUtils.getManager(request);
        // 判断是否有权限
        if(!LimitUtils.hasPower(manager, Rule.UPLOAD_CONFIG_RULE)) {
            log.warn(ResultCode.FAILED_MANAGER_NOT_POWERS.toString());
            return AppResult.failed(ResultCode.FAILED_MANAGER_NOT_POWERS);
        }
        // 调用service层
        pageConfigService.saveConfig();
        // 返回成功
        return AppResult.success();
    }

}
